package no.ntnu.fp.handlers;

import no.ntnu.fp.client.Client;
import no.ntnu.fp.handler.Handler;
import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.impl.LoginSuccessfulMessage;
import no.ntnu.fp.model.CalendarModel;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.model.LoggedInModel;
import no.ntnu.fp.model.NoticeModel;

public class LoginSuccessfulHandler implements Handler {
	
	private final CalendarModel calendarModel;
	private final LoggedInModel loggedInModel;
	private final NoticeModel noticeModel;
	
	public LoginSuccessfulHandler(CalendarModel calendarModel, 
			NoticeModel noticeModel, LoggedInModel loggedInModel) {
		this.calendarModel = calendarModel;
		this.noticeModel = noticeModel;
		this.loggedInModel = loggedInModel;
	}

	@Override
	public void handleMessage(Message message) {
		LoginSuccessfulMessage loginSuccessfulMessage = (LoginSuccessfulMessage)message;

		Employee employee = loginSuccessfulMessage.getEmployee();
		Client.setEmployee(employee);
		
		calendarModel.setAppointmentsForEmployee(employee, loginSuccessfulMessage.getAppointments());
		calendarModel.setMeetingsForEmployee(employee, loginSuccessfulMessage.getMeetings());
		noticeModel.setNotices(loginSuccessfulMessage.getNotices());
		
		loggedInModel.setEmployee(employee);
		loggedInModel.setLoggedIn(true, null);
	}
}
